<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model"  :rules="validatorRules" slot="detail">
        <!-- useful_auto_generate_section_start -->

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="租户"  :labelCol="labelCol_2" :wrapperCol="wrapperCol_2" >
              <a-select v-model="model.tenantId" :options="tenantIdOptions"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="名称"  :labelCol="labelCol_2" :wrapperCol="wrapperCol_2" >
              <a-input v-model="model.name" placeholder="请输入名称" />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="8">
            <a-form-model-item label="分类"  :labelCol="labelCol_3" :wrapperCol="wrapperCol_3" >
              <a-select v-model="model.category" :options="categoryOptions"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="时间周期"  :labelCol="labelCol_3" :wrapperCol="wrapperCol_3" >
              <a-select v-model="model.period" :options="periodOptions" @change="onPeriodChange"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="时间表达式"  :labelCol="labelCol_3" :wrapperCol="wrapperCol_3" >
              <a-input v-model="model.cron" placeholder="请输入时间表达式"  />
            </a-form-model-item>
          </a-col>
        </a-row>


        <a-row>
          <a-col :span="24">
            <a-form-model-item label="参数"  :labelCol="labelCol_1" :wrapperCol="wrapperCol_1" >
              <a-textarea v-model="model.param" placeholder="请输入参数" />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="描述"  :labelCol="labelCol_1" :wrapperCol="wrapperCol_1" >
              <a-textarea v-model="model.description" placeholder="请输入描述" />
            </a-form-model-item>
          </a-col>
        </a-row>

      <!-- useful_auto_generate_section_end -->

      </a-form-model>
    </j-form-container>
  </a-spin>
</template>


<script>

  import { httpAction, getAction } from '@/api/manage'
  import { getDataOptionList } from '@/utils/util'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import JUpload from '@/components/jeecg/JUpload'
  import { FormMixin } from '@/mixins/FormMixin'
  import { requestSetData, requestSetOptions } from '@/utils/commonUtils'
  import moment from 'moment'

  export default {
    name: 'JobTemplateForm',
    mixins: [ FormMixin],
    components: {
      ATextarea
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        confirmLoading: false,
        validatorRules: {
        },
        //useful_auto_generate_section_start
        tenantIdOptions:[],
        categoryOptions:getDataOptionList('jobTemplateCategory'),
        periodOptions:getDataOptionList('jobTemplatePeriodEnum'),
        statusOptions:getDataOptionList('jobTemplateStatusEnum'),
        enabledOptions:getDataOptionList('yesNoNumber'),
        //useful_auto_generate_section_end
        url: {
          add: "/admin/jobTemplate/save",
          edit: "/admin/jobTemplate/update",
          queryById: "/admin/jobTemplate/detail"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      let $this = this;
      //useful_auto_generate_section_start
      requestSetOptions("/user/anonymous/tenant/listAll",{},function(options){$this.tenantIdOptions=options;},{label:'name',value:'id'});
      //useful_auto_generate_section_end

       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));


    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      handleChange(value) {
        // console.log(`selected `);
      },
      onChange(e) {
        console.log('radio1 checked', e.target.value);
      },
      onPeriodChange(val){
        // debugger;
        console.log(val);
        if(val == -1){
          this.model.cron = "* * * * * ? *";
          return;
        }
        let cron = "";
        if(val == 0){
          cron = "*/1 * * * * ? *"
        }else if(val == 1){
          cron = "5 */1 * * * ? *"
        }else if(val == 2){
          cron = "5 0 */1 * * ? *"
        }else if(val == 3){  //天
          cron = "5 0 0 */1 * ? *"
        }else if(val == 4){  //周 (每周一)
          cron = "5 0 0 ? * 1 *"
        }else if(val == 5){  //月
          cron = "5 0 0 1 1/1 ? *"
        }else if(val == 6){ //季度
          cron = "5 0 0 1 1/3 ? *"
        }else if(val == 7){  //年
          cron = "5 0 0 1 1 ? *"
        }

        this.model.cron = cron;


      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid=>{
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            if(!this.model.id){
              httpurl+=this.url.add;
            }else{
              httpurl+=this.url.edit;
            }
            httpAction(httpurl,this.model,"post").then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>
